package PTA.javaA;

import java.util.Arrays;
import java.util.Scanner;

public class ex07 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        String pe[] = new String[n];//存身份证号
        String birth[] = new String[n];//存生日
        String line;
        for(int i=0;i<n;i++){
            pe[i]=sc.next();//把身份证存入数组
        }
        while(true){
            line = sc.next();
            if(line.equals("sort1")){
                for(int i=0;i<n;i++){
                    birth[i]=pe[i].substring(6,10)+"-"+pe[i].substring(10,12)+"-"+pe[i].substring(12,14);
                }
                Arrays.sort(birth);
                for(int i=0;i<n;i++){
                    System.out.println(birth[i]);
                }
            }else if(line.equals("sort2")){
                for(int i=0;i<n;i++){
                    birth[i]=pe[i].substring(6,10)+pe[i].substring(10,12)+pe[i].substring(12,14);
                }
                Arrays.sort(birth);
                for(int i=0;i<n;i++){
                   for(int j =0;j<n;j++){
                       if(pe[j].contains(birth[i])){
                           System.out.println(pe[j]);
                           break;
                       }
                    }
                }
            }else if(line.equals("e")){
                System.out.println("exit");
                break;
            }



        }








    }
}
